#include "mainwindow_game1.h"
#include "ui_mainwindow_game1.h"
#include <QDebug>
MainWindow_game1::MainWindow_game1(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow_game1)
{
    game1_scene();
}

MainWindow_game1::~MainWindow_game1()
{
    delete ui;
    QTimer *_time=new QTimer();
   _time->start(5);
    connect(_time,SIGNAL(timeout()),this,SLOT(player_move()));
//    qDebug()<<"lalala";

}

void MainWindow_game1::keyPressEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Up) m_player1.flag[0]=1;
    if(e->key()==Qt::Key_Down) m_player1.flag[1]=1;
    if(e->key()==Qt::Key_Left) m_player1.flag[2]=1;
    if(e->key()==Qt::Key_Right) m_player1.flag[3]=1;
    update();
}

void MainWindow_game1::keyReleaseEvent(QKeyEvent *e)
{
    if(e->key()==Qt::Key_Up) m_player1.flag[0]=0;
    if(e->key()==Qt::Key_Down) m_player1.flag[1]=0;
    if(e->key()==Qt::Key_Left) m_player1.flag[2]=0;
    if(e->key()==Qt::Key_Right) m_player1.flag[3]=0;
    update();
//        qDebug()<<"lalala"<<m_player1.flag[0];
}



void MainWindow_game1::game1_scene()
{
    //设置窗口尺寸（固定）
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //标题
    setWindowTitle(GAME1_TITLE);
    //窗口图标
    setWindowIcon(QIcon(GAME_Icon));
//        qDebug()<<"lalala";
}

void MainWindow_game1::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawPixmap(m_map2.m_map1_posx,m_map2.m_map1_posy,m_map2.m_map1_2);//  尝试绘制地图场景

//    painter.setPen(QPen(Qt::red,2));
//    painter.drawRect(x,y,10,10);
    //绘制角色
    painter.drawPixmap(m_player1.player_x,m_player1.player_y,m_player1.m_player);
//    qDebug()<<"lalala";
}

void MainWindow_game1::UPDATE()
{
    update();
    //    qDebug()<<"lalala";
}

void MainWindow_game1::player_move()
{
    if(m_player1.flag[0]==1)m_player1.player_y--;
    if(m_player1.flag[1]==1)m_player1.player_y++;
    if(m_player1.flag[2]==1)m_player1.player_x--;
    if(m_player1.flag[3]==1)m_player1.player_x++;
    update();

}

